<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" :inline="true"  ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="产品" prop="product">
          <el-input v-model="dataForm.product" placeholder="产品"></el-input>
      </el-form-item>
          <el-form-item label="产品编码" prop="productCode">
          <el-input v-model="dataForm.productCode" placeholder="产品编码"></el-input>
      </el-form-item>
          <el-form-item label="产品公司" prop="partyChannel">
          <el-input v-model="dataForm.partyChannel" placeholder="甲方渠道"></el-input>
      </el-form-item>
      <el-form-item label="渠道" prop="channel">
          <el-input v-model="dataForm.channel" placeholder="渠道"></el-input>
      </el-form-item>
          <el-form-item label="渠道公司" prop="secondChannel">
          <el-input v-model="dataForm.secondChannel" placeholder="乙方渠道"></el-input>
      </el-form-item>
      <el-form-item label="转化数量" prop="resultNumber">
          <el-input v-model="dataForm.resultNumber" placeholder="转化数量"></el-input>
      </el-form-item>
      <el-form-item label="甲方单价" prop="partyPrice">
          <el-input v-model="dataForm.partyPrice" placeholder="甲方单价"></el-input>
      </el-form-item>
      <el-form-item label="甲方结算" prop="partySetter">
          <el-input v-model="dataForm.partySetter" placeholder="甲方结算金额"></el-input>
      </el-form-item>
      <el-form-item label="乙方单价" prop="secondPrice">
          <el-input v-model="dataForm.secondPrice" placeholder="乙方单价"></el-input>
      </el-form-item>
      <el-form-item label="乙方结算" prop="secondSetter">
          <el-input v-model="dataForm.secondSetter" placeholder="乙方结算"></el-input>
      </el-form-item>
          <el-form-item label="利润" prop="profit">
          <el-input v-model="dataForm.profit" placeholder="利润"></el-input>
      </el-form-item>
      
          <el-form-item label="执行商务" >
            <el-select v-model="dataForm.directBusiness" placeholder="选择商务人员" filterable clearable class="selectCls" >
              <el-option v-for="item in businessList " :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="对接商务" >
            <el-select v-model="dataForm.joinBusiness" placeholder="选择商务人员" filterable clearable class="selectCls">
              <el-option v-for="item in businessList " :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
              </el-option>
            </el-select>
          </el-form-item>
          
          <el-form-item label="运营">
            <el-select v-model="dataForm.operate" placeholder="选择运营人员" filterable clearable class="selectCls">
              <el-option v-for="item in userNameList " :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
              </el-option>
            </el-select>
          </el-form-item>
        
          <el-form-item label="日期" prop="day" >
          <el-date-picker v-model="dataForm.day" value-format="yyyy-MM-dd" type="date"
            placeholder="起始时间" class="selectCls">
          </el-date-picker>
      </el-form-item>
         
          <el-form-item label="业务类型" >
            <el-select v-model="dataForm.type" placeholder="选择运营人员" filterable clearable class="selectCls">
              <el-option v-for="item in typeList " :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
              </el-option>
            </el-select>
      </el-form-item>
      </el-form>
      <el-form>
        <el-form-item label="备注" >
          <el-input type="textarea" :rows="4"  v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import { getDictDataList } from '@/utils'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        product: '',
        productCode: '',
        partyChannel: '',
        secondChannel: '',
        profit: '',
        directBusiness: '',
        directBusinessProfit: '',
        joinBusiness: '',
        joinBusinessProfit: '',
        operate: '',
        operateProfit: '',
        day: '',
        month: '',
        createTime: '',
        updateTime: '',
        type: ''
      },
      userNameList: [],
      businessList: [],
      typeList:[]
    }
  },
  computed: {
    dataRule () {
      return {
        product: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        partyChannel: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        secondChannel: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        profit: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        directBusiness: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        type: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getNameDict();
    this.getBusinessDict();
    this.getTypeList();
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    getNameDict() {
        this.userNameList = getDictDataList("userNameList")
    },
    getBusinessDict() {
      this.businessList = getDictDataList("businessList")
    },
    getTypeList(){
      this.typeList=getDictDataList("bizTypeList")
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/adx/totalstatistics/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/adx/totalstatistics/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>

<style scoped>
.selectCls{
  width: 194px;
}
</style>